﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using SD.LLBLGen.Pro.ORMSupportClasses;
using WMS.HelperClasses;
using WHMS.HelperClasses;
using System.Collections;
using WHMS.EntityClasses;

namespace WMS
{
	public partial class ItemPickerForm : Form
	{
		public string SelectedItemName { get; set; }
		public int SelectedItemId { get; set; }
		public int ItemType { get; set; }
        public bool ShowAllItemTypes=false;
        public PredicateExpression _ProjectsFilter=null;


        private enum SearchType { Code, Name };

		public ItemPickerForm(int itemType)
		{
			InitializeComponent();

			ItemType = itemType;			
		}

        public ItemPickerForm(int itemType, int searchMethod, PredicateExpression ProjectsFilter)
        {
            InitializeComponent();

            ItemType = itemType;

            _ProjectsFilter = ProjectsFilter;

            ShowAllItemTypes = true;

            if (searchMethod == (int)SearchType.Code)
            {
                SearchByCodeCB.Checked = true;
            }
            else
                SearchByNameCB.Checked = true;
        }

		private void RunBtn_Click(object sender, EventArgs e)
		{
            var fields = new ResultsetFields(4);
            SortExpression sorter = null;
            var filter = new RelationPredicateBucket();

            if (_ProjectsFilter != null)
            {
                fields.DefineField(ItemFields.Id, 0, "Id");
                fields.DefineField(ItemFields.Code, 1, "Code");
                fields.DefineField(ItemFields.Name, 2, "Name");
                fields.DefineField(UnitFields.Name, 3, "Unit");


                filter.Relations.Add(StoreShelfBalanceEntity.Relations.ItemEntityUsingItemId);
                //filter.Relations.Add(ItemEntity.Relations.ItemTypeEntityUsingTypeId);
                filter.Relations.Add(ItemEntity.Relations.UnitEntityUsingUnitId);
                filter.PredicateExpression.Add(_ProjectsFilter);


                if (SearchByCodeCB.Checked && !string.IsNullOrEmpty(SearchCodeTB.Text))
                {
                    var codeTerm = SearchCodeTB.Text.Trim() + "%";
                    filter.PredicateExpression.AddWithAnd(ItemFields.Code % codeTerm);

                    sorter = new SortExpression(ItemFields.Code | SortOperator.Ascending);
                }

                if (SearchByNameCB.Checked && !string.IsNullOrEmpty(SearchNameTB.Text))
                {
                    var nameTerm = SearchNameTB.Text.Trim() + "%";
                    filter.PredicateExpression.AddWithAnd(ItemFields.Name % nameTerm);

                    sorter = new SortExpression(ItemFields.Name | SortOperator.Ascending);
                }

                var dt1 = DALHelper.GetDynamicList(fields, filter, sorter);
                dataGridView1.DataSource = dt1;

                if (dt1.Rows.Count > 0)
                {
                    dataGridView1.Rows[0].Selected = true;
                }

            }
            else
            {


                fields.DefineField(ItemFields.Id, 0, "Id");
                fields.DefineField(ItemFields.Code, 1, "Code");
                fields.DefineField(ItemFields.Name, 2, "Name");
                fields.DefineField(UnitFields.Name, 3, "Unit");



                filter.Relations.Add(ItemEntity.Relations.ItemTypeEntityUsingTypeId);
                filter.Relations.Add(ItemEntity.Relations.UnitEntityUsingUnitId);

                if (ShowAllItemTypes == false)
                    filter.PredicateExpression.Add(ItemFields.TypeId == ItemType);


                if (SearchByCodeCB.Checked && !string.IsNullOrEmpty(SearchCodeTB.Text))
                {
                    var codeTerm = SearchCodeTB.Text.Trim() + "%";
                    filter.PredicateExpression.Add(ItemFields.Code % codeTerm);

                    sorter = new SortExpression(ItemFields.Code | SortOperator.Ascending);
                }

                if (SearchByNameCB.Checked && !string.IsNullOrEmpty(SearchNameTB.Text))
                {
                    var nameTerm = SearchNameTB.Text.Trim() + "%";
                    filter.PredicateExpression.Add(ItemFields.Name % nameTerm);

                    sorter = new SortExpression(ItemFields.Name | SortOperator.Ascending);
                }

                var dt = DALHelper.GetDynamicList(fields, filter, sorter);
                dataGridView1.DataSource = dt;

                if (dt.Rows.Count > 0)
                {
                    dataGridView1.Rows[0].Selected = true;
                }
            }
		}

		private void OkBtn_Click(object sender, EventArgs e)
		{
			if (dataGridView1.SelectedRows.Count > 0)
			{
				var name = (string)dataGridView1.SelectedRows[0].Cells[2].Value;
				var code = (string)dataGridView1.SelectedRows[0].Cells[1].Value;
				var unit = (string)dataGridView1.SelectedRows[0].Cells[3].Value;

				SelectedItemName = name + " - " + code + " - (" + unit + ")";
				SelectedItemId = (int)dataGridView1.SelectedRows[0].Cells[0].Value;
			}

			this.DialogResult = DialogResult.OK;

			this.Close();
		}

		private void SearchByCodeCB_CheckedChanged(object sender, EventArgs e)
		{
			SearchCodeTB.Enabled = SearchByCodeCB.Checked;
		}

		private void SearchByNameCB_CheckedChanged(object sender, EventArgs e)
		{
			SearchNameTB.Enabled = SearchByNameCB.Checked;
		}

	}
}
